package com.test.controller;

import com.test.entity.Adminuser;
import com.test.service.AdminuserService;
import com.test.service.impl.AdminuserServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin")
public class LoginCtrl {

    //@Autowired
    @Resource
    AdminuserService adminuserService;

    @GetMapping("/goLogin")
    public String goLogin(){
        // return "../templates/login.html";
        return "login";
    }

    @PostMapping("/doLogin")
    public String doLogin(Adminuser adminuser, Model model, HttpSession session){
        Adminuser user = adminuserService.getAdminUser(adminuser);
        if(user == null){
            model.addAttribute("msg","用户名或者密码不正确");
            return "login";
        }
        session.setAttribute("aduName",user.getName());
        return "redirect:/admin/goods/getGoodsList";
    }

    @GetMapping("/logout")
    public String doLogout(HttpSession session){

        session.removeAttribute("aduName");
        session.invalidate();
        return "login";
    }

}
